<template>
	<div style="height: 100vh; overflow:hidden; display:flex; align-items:center; justify-content:center; background-color:#95f094;">
		<div style="display: flex;background-color:white;width: 50%; border-radius: 5px; overflow: hidden">
			<div style="flex:1">
				<img src="../../static/register.png" alt="" style="width:100%">
			</div>
			<div style="flex:1;display: flex;align-items: center;justify-content: center;">
				<el-form :model="user" style="width: 80%;" :rules="rules" ref="registerRef">
					<div style="font-size: 20px;font-weight: bold;text-align: center;margin-bottom: 20px;">欢迎注册万物互联平台</div>
					<el-form-item prop="UserName">
						<el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="user.UserName"></el-input>
					</el-form-item>
					<el-form-item prop="UserPwd">
						<el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码" v-model="user.UserPwd"></el-input>
					</el-form-item>
					<el-form-item prop="confirmPass">
						<el-input prefix-icon="el-icon-lock" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" style="width: 100%;" @click="register">注册</el-button>
					</el-form-item>
					<div style="display: flex;">
						已经有账号？请<span style="color: #87CFFF;cursor: pointer;" @click="$router.push('/')">登录</span>
					</div>
				</el-form>
			</div>
		</div>
	</div>
</template>

<script>
//import axios from "axios";
export default {

	name: "RegisterUser",
	data() {
		const validatePassword = (rule, confirmPass, callback) => {
			if (confirmPass === '') {
				callback(new Error('请确认密码'))
			} else if (confirmPass !== this.user.UserPwd) {
				callback(new Error('两次输入密码不一致'))
			} else {
				callback()
			}
		}

		return {
			user: {
				UserName: '',
				UserPwd: '',
				confirmPass: ''
			},
			rules: {
				UserName: [{
					required: true, message: '请输入用户名', trigger: 'blur'
				}],
				UserPwd: [{
					required: true, message: '请输入密码', trigger: 'blur'
				}],
				confirmPass: [{
					validator: validatePassword, trigger: 'blur'
				}],
			}
		}
	},
	methods: {
		register() {
			this.$refs['registerRef'].validate((valid) => {
				if (valid) {
					//验证通过才能请求
					this.$request.post('/register', this.user).then(res => {
						//const res = await axios.post(' http://localhost:8000/register', this.user);
						console.log(res);
						if (res.code === '200') {
							this.$router.push('/')
							this.$message.success('注册成功')
						} else {
							this.$message.error(res.msg)
						}
					})
				}
			})
		}
	}
}
</script>
  
<style>
	
</style>